<?php
ob_start();
session_start();

require_once('Admin_Generator.php');
require_once('libraries/Pagination.php');


$user = new Volta_User();
$user -> set_login($_POST['login']);
$user -> set_password($_POST['password']);

$auth = new Volta_Auth($user);

try
{
	if($auth -> is_logged())
	{
		try
		{
			$admin = new Volta_Admin_Generator('news');
			//ustawiamy relacje
			$rel = $admin -> relationship_one_to_many('comment_news', 'news_id');
			$admin -> set_relationship_allowed_columns(array('id', 'autor', 'data'));
			$admin -> add_validation_rules('autor', array('str' => array('field' => 'autor', 'required' => true, 'between' => array(3,6))));
			//ustawiamy widok formularzy
			$admin -> set_render_form_view('Standard');
			//ustawiamy widok panelu
			$admin -> set_render_panel_view('Table_Standard');
			//wykonujemy odpowiednie akcje
			print $admin -> execute_action();
			print $admin -> render_panel();
			print $admin -> paginate();
		}
		catch(Volta_Admin_Generator_NoRows_Exception $e)
		{
			print $e -> getMessage();
		}
		catch(Volta_Admin_Generator_NoAction_Exception $e)
		{
			print $e -> getMessage();
		}
		catch(Exception $e)
		{
			print($e -> getMessage() . '\n' . $e -> getTraceAsString());
		}

		if(isset($_GET['relationship']))
			print $admin -> show_panel_relationship($rel);
		//$admin -> relationship_many_to_many('users', array('users.name', 'group.name'), array('groups' => array('comment.news_id' => 'news.id'), 'qwerty' => array('zxc.news_id' => 'asd.id')))
	}
}
catch(Volta_Auth_Exception $e)
{
	$form = Volta_Form::open_with_table();
	$form .= Volta_Form::text('login', 'Login');
	$form .= Volta_Form::password('password');
	$form .= Volta_Form::submit('log_in', 'Zaloguj');
	$form .= Volta_Form::close_with_table();
	print Volta_Form::get_form();
}
	

if(isset($_POST['submit_log_in']))
{
	$validation = new Volta_Validator();
	$validation -> load('str');
	$validation -> add_data($_POST);
	$validation -> add_rule('login', new str(array('field' => 'login', 'required' => true, 'alphadigit' => true)));
	$validation -> add_rule('password', new str(array('field' => 'haslo', 'required' => true, 'alphadigit' => true)));
	$validation -> validation();

	if(sizeof($validation -> get_errors()) == 0)
	{
		if($auth -> login())
			header("Location: /formbuilider/index.php");
		else
			print('<div align="center" style="padding:5px;margin:5px;background-color:#C0C0C0;border:1px solid #ff0033;"><b>Nieprawidlowy login lub haslo</b></div>');
	}
	
	else
	{
		foreach($validation -> get_errors() as $data => $err)
			print('<div align="center" style="padding:5px;margin:5px;background-color:#C0C0C0;border:1px solid #ff0033;"><b>'.$err.'</b></div>');
	}
}



?>